#include "tyledHttp.h"

#ifdef CONSOLE_APP

int main(int argc, char **argv)
{
	Tyle_init();
	if (HttpServer::start(argc,argv)==0) return -1;
	int c=getchar();
  return HttpServer::stop();
}

#else

#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <assert.h>
#include <shellapi.h>
#include <commctrl.h>

#include "resource.h"

#define WM_TRAY (WM_APP + 1)
#define ID_TRAY 5000

#define ID_TRAY_SLOWLOG	6000
#define ID_TRAY_EXIT	6001

LRESULT CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

// Foward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
bool				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);

//# Global variables
NOTIFYICONDATA g_iconData;	//# Tray icon structure
HICON g_hIcon;				//# Tray icon handle
HINSTANCE g_hInst;			//# Application instance
HMENU g_hMenu;				//# Handle to tray popup menu
bool g_bModelDlg;			//# Flag indicating Add Timezone dialog displayed or not
int g_nChecked;				//# Stores the currently checked item in the menu
static int port;

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG msg;
	Tyle_init();

	{
		g_hInst = hInstance;

		WNDCLASSEX wcex;

		memset(&wcex, 0, sizeof(WNDCLASSEX));

		wcex.cbSize = sizeof(WNDCLASSEX);

		wcex.lpfnWndProc	= (WNDPROC)WndProc;
		wcex.hInstance		= hInstance;
		wcex.lpszClassName	= "TYLEDHTTP";
		wcex.hIconSm		= LoadIcon((HINSTANCE__*)wcex.hInstance, (LPCTSTR)IDI_SMALL);

		g_hIcon = wcex.hIconSm;

		RegisterClassEx(&wcex);
	}

	// Perform application initialization:

	{
		HWND hWnd;

		//# Create hidden window
		hWnd = CreateWindow("TYLEDHTTP", 0, WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

		if (!hWnd)
			return 0;

		//# Set initial timer
		//SetTimer(hWnd, 1, 1000, 0);

  	{	int argc;
  		LPWSTR *wargv=CommandLineToArgvW(GetCommandLineW(), &argc);	
  		LPSTR *argv=new char *[argc];
  		for(int i=0; i<argc; i++) 
  		{	argv[i]=new char[256];
  			WideCharToMultiByte(CP_ACP, 0, wargv[i],-1,argv[i],256,0,0);
  		}
  		port = HttpServer::start(argc,argv);
  	}

		//# Create tray icon
		g_iconData.cbSize = sizeof(NOTIFYICONDATA);
		g_iconData.hIcon = g_hIcon;
		g_iconData.hWnd = hWnd;
		sprintf(g_iconData.szTip, "TyledHttp at port %i", port);
		g_iconData.uCallbackMessage = WM_TRAY;
		g_iconData.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
		g_iconData.uID = ID_TRAY;

		Shell_NotifyIcon(NIM_ADD, &g_iconData);		//# Add icon to tray

		//# Create popup menu and add initial items
		g_hMenu = CreatePopupMenu();

		//AppendMenu(g_hMenu, MF_SEPARATOR, 0, 0);
		AppendMenu(g_hMenu, MF_STRING, ID_TRAY_SLOWLOG, "Slow Log");
		AppendMenu(g_hMenu, MF_SEPARATOR, 0, 0);
		AppendMenu(g_hMenu, MF_STRING, ID_TRAY_EXIT, "Exit");

		//# Make Add Timezone bold
		SetMenuDefaultItem(g_hMenu, ID_TRAY_SLOWLOG, FALSE);

    if (port==0) 
    { DestroyWindow(hWnd);
    }

	}

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

/*
//# Procedure to handle messages to the dialog box
LRESULT CALLBACK DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
	case WM_INITDIALOG:
		DlgFunc::OnInitDialog(hDlg, message, wParam, lParam);
		break;

	case WM_COMMAND:
		DlgFunc::OnCommand(hDlg, message, wParam, lParam);
		break;

	case WM_NOTIFY:
		DlgFunc::OnNotify(hDlg, message, wParam, lParam);
		break;
	}

	return 0;
}
*/

void TrayMessageHandler(HWND hWnd, UINT uMessageID)
{
	switch (uMessageID)
	{
	case ID_TRAY_EXIT:
		DestroyWindow(hWnd);
		break;
	default:
		Tyle::Object("showLog!;");	
		break;
	}
}

//# Procedure to handle messages to the main hidden window
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
	case WM_COMMAND:
		TrayMessageHandler(hWnd, LOWORD(wParam));
		break;

	case WM_DESTROY:
		Tyle::Object(100, "puttl('wndMessage(0x%x)');", (int)message);
	  if (port)
		  HttpServer::stop();
	  DestroyMenu(g_hMenu);
	  Shell_NotifyIcon(NIM_DELETE, &g_iconData);
		PostQuitMessage(0);
		break;

	case WM_TRAY:
		if (WM_RBUTTONDOWN == lParam)
		{
			POINT pt;
			GetCursorPos(&pt);
			
			SetForegroundWindow(hWnd);
			
			TrackPopupMenu(g_hMenu, 0, pt.x, pt.y, 0, hWnd, 0);
			
			PostMessage(hWnd, WM_NULL, 0, 0);
		}
		else if (WM_LBUTTONDBLCLK == lParam)
		{
			TrayMessageHandler(hWnd, ID_TRAY_SLOWLOG);
		}
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return 0;
}

#endif
